﻿<%@ WebHandler Language="C#" Class="Org" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Org : IHttpHandler,IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public string ErrMsg { get; set; }
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["navtitle"], context.Request["ptag"],
                        context.Request["url"], context.Request["parentid"], context.Request["iconcls"],
                        context.Request["visible"],context.Request["sortnum"]))
                    {
                        context.Response.Write(success("", "NavList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "NavList"));
                    }
                    else
                    {
                        context.Response.Write(failure(ErrMsg));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["navid"], context.Request["navtitle"], context.Request["ptag"],
                        context.Request["url"], context.Request["parentid"], context.Request["iconcls"],
                        context.Request["visible"], context.Request["sortnum"]))
                    {
                        context.Response.Write(success("", "NavList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
                case "addNavPermision":
                    if (addNavPermision(context.Request["navid"], context.Request["pidstr"]))
                    {
                        context.Response.Write(success("", "NavList"));
                    }
                    else
                    {
                        context.Response.Write(failure("添加操作失败!"));
                    }
                    break;
            }
        }
    }

    public bool Del(string id)
    {
        var navManager = appContext.GetObject("S_NavManager") as GONES.IBLL.IS_NavManager;
        try
        {
            if (navManager.GetNavCountByParentId(id) > 0)
            {
                ErrMsg = "该菜单下存在子菜单不能删除！";
                return false;
            }
            navManager.Delete(id);
            return true;
        }
        catch { return false; }
    }



    private GONES.Model.S_Nav GetNavModel(string navid, string title,string ptag,string url, string parentid, string iconcls,string visible,string sortnum)
    {
        var navManager = appContext.GetObject("S_NavManager") as GONES.IBLL.IS_NavManager;
        GONES.Model.S_Nav nav = new GONES.Model.S_Nav();
        visible = visible == "on" ? "0" : "1";
        if (navid.Trim() != "")
            nav = navManager.Get(navid.Trim());
        nav.ID = navid.Trim();
        nav.Title = title;
        nav.Ptag = ptag;
        nav.Url = url;
        nav.Parentid = parentid.Trim();
        nav.Iconcls = iconcls.Trim();
        nav.SortNum =int.Parse(sortnum.Trim());
        return nav;
    }
    public bool Add( string title, string ptag, string url, string parentid, string iconcls, string visible, string sortnum)
    {
        var navManager = appContext.GetObject("S_NavManager") as GONES.IBLL.IS_NavManager;
        GONES.Model.S_Nav nav = new GONES.Model.S_Nav();
        try
        {
            nav = GetNavModel("", title, ptag, url, parentid, iconcls, visible, sortnum);
            nav.ID = GONES.Tools.Common.Rnd.Guid.ToString().Trim();
            nav.CreateUser = loginInfo.UserName.Trim();
            navManager.Add(nav);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string navid, string title, string ptag, string url, string parentid, string iconcls, string visible, string sortnum)
    {
        var navManager = appContext.GetObject("S_NavManager") as GONES.IBLL.IS_NavManager;
        GONES.Model.S_Nav nav = new GONES.Model.S_Nav();
        try
        {
            nav = GetNavModel(navid, title, ptag, url, parentid, iconcls, visible, sortnum);
            nav.ModifyUser = loginInfo.UserName;
            navManager.Edit(nav);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool addNavPermision(string navid,string pidstr)
    {
        if (pidstr == "1")
            return true;
        var navPermissionManager = appContext.GetObject("S_NavPermissionManager") as GONES.IBLL.IS_NavPermissionManager;
        GONES.Model.S_NavPermission navPermission = null;
        try
        {
            System.Collections.Generic.IList<GONES.Model.S_NavPermission> permissionsList = navPermissionManager.GetNavPermissionList(navid);
            foreach (var navPer in permissionsList)
            {
                navPermissionManager.Delete(navPer.ID);
            }
            if (pidstr != "")
            {
                string[] pid = pidstr.Split(',');
                for (int i = 0; i < pid.Length; i++)
                {
                    navPermission = new GONES.Model.S_NavPermission();
                    navPermission.NavId = navid;
                    navPermission.PermisionId = pid[i].Trim();
                    navPermission.ID = GONES.Tools.Common.Rnd.Guid;
                    navPermissionManager.Add(navPermission);
                }
            }
            return true;
        }
        catch
        {
            return false; 
        }
    }
    
    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}